<script lang="ts">
import Vue from 'vue'
import {editOrUpdateWord} from '@/api/wordSystemLibraryTypeWord'


export default Vue.extend({
  name: "addWord",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    wordForm: {
      type: Object,
      default: () => {
        return {
          id: null,
          fullSpell: '',
          rootSummary: '',
          difficultyLevel: '',
          occurrence: '',
        }
      }
    }
  },
  data() {
    return {
      dialogTitle: '新增单词',
      levelOptions: [],
      rules: {
        fullSpell: [{required: true, message: '单词全拼', trigger: 'blur'}],
        wordMeaning: [{required: true, message: '单词释义', trigger: 'blur'}]
      }
    }
  },
  created() {
    if (this.wordForm.id) {
      this.dialogTitle = '修改单词'
    }
    if (!this.wordForm.fullSpell){
      this.wordForm.fullSpell = this.wordForm.wordFullSpell
    }
    this.generateLevelOptions()
  },
  methods: {
    generateLevelOptions() {
      this.levelOptions = Array.from({length: 25}, (_, i) => `${i + 1}k`)
    },
    handleSubmit() {
      this.$refs.wordForm.validate(valid => {
        if (valid) {
          editOrUpdateWord(this.wordForm).then(() => {
            this.$emit('update:visible', false)
            if (this.wordForm.id) {
              this.$message.success('更新成功')
            } else {
              this.$message.success('新增成功')
            }
          })
        }
      })
    }
  }
})
</script>

<template>
  <!-- 弹窗表单 -->
  <el-dialog :title="dialogTitle" :visible.sync="visible" width="500px" :close-on-click-modal="false">
    <el-form :model="wordForm" :rules="rules" ref="wordForm" label-width="100px">
      <el-form-item label="单词全拼" prop="fullSpell">
        <el-input v-model="wordForm.fullSpell"/>
      </el-form-item>
      <el-form-item label="单词释义" prop="wordMeaning">
        <el-input v-model="wordForm.wordMeaning"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
